*************************************
* GENERATING MONADIC TRADE ABBA TERMS
*************************************
cd "C:\\..."


use "mirrortrade_dyadic.dta", clear
* generate counter
generate num=1

* generate countryyear variable 
generate countryyearA=countryA_name + "_" +string(year)
generate countryyearB=countryB_name + "_" +string(year)

**************************
* IMPORTS (flow 1, B to A)
**************************

* counters
sort countryyearA countryB_name (year)

*by countryyearA: egen num_obs=sum(num)		// old code, but always the same as sum()
by countryyearA: egen num_obs=total(num)

by countryyearA: egen num_impBAA=total(num) if flowBAA!=.
by countryyearA: egen num_impBAB=total(num) if flowBAB!=.


by countryyearA: generate consistent_impBA=1 if num_impBAA!=. & num_impBAB!=.
by countryyearA: generate consistentnopr_impBA=1 if consistent_impBA==1 & pr_flowBAA==0 & pr_flowBAB==0  

* values

by countryyearA: egen totval_impBAA=sum(flowBAA)

by countryyearA: egen totval_impBAB=sum(flowBAB)

by countryyearA: egen totval_impBAA_cons=sum(flowBAA) if consistent_impBA==1
by countryyearA: egen totval_impBAA_cons2=max(totval_impBAA_cons)

by countryyearA: egen totval_impBAB_cons=sum(flowBAB) if consistent_impBA==1
by countryyearA: egen totval_impBAB_cons2=max(totval_impBAB_cons)

by countryyearA: egen totval_impBAA_consnopr=sum(flowBAA) if consistentnopr_impBA==1
by countryyearA: egen totval_impBAA_consnopr2=max(totval_impBAA_consnopr)

by countryyearA: egen totval_impBAB_consnopr=sum(flowBAB) if consistentnopr_impBA==1
by countryyearA: egen totval_impBAB_consnopr2=max(totval_impBAB_consnopr)

label variable totval_impBAA_consnopr2 "Monadic aggregate mirror imports, home"
label variable totval_impBAB_consnopr2 "Monadic aggregate mirror imports, partners"


** I use string labels instead of numeric codes
keep if countryB_alpha2cc == "US" | countryB_alpha2cc == "CA"
drop if countryB_alpha2cc == "CA" & countryA_alpha2cc != "US"


keep countryA_alpha2cc countryA_name year totval_impBAA_consnopr2 totval_impBAB_consnopr2

save "monadicimports.dta", replace

**************************
* EXPORTS (flow 2, A to B)
**************************

*use mirrortrade-uni-wgtavgs
use mirrortrade_dyadic.dta, clear

* generate counter

generate num=1

generate countryyearA=countryA_alpha2cc + "_" +string(year)
generate countryyearB=countryB_alpha2cc + "_" +string(year)


* Note that, compared to imports, A and B switch places:  B=home&sender; A=partner&receiver -> flow1=partner; flow1mirror="home"

* counters

sort countryyearB countryA_alpha2cc

by countryyearB: egen num_obs_exp=sum(num)

by countryyearB: egen num_expBAA=sum(num) if flowBAA!=.
by countryyearB: egen num_expBAB=sum(num) if flowBAB!=.
by countryyearB: generate consistent_expBA=1 if num_expBAA!=. & num_expBAB!=.
by countryyearB: generate consistentnopr_expBA=1 if consistent_expBA==1 & pr_flowBAA==0 & pr_flowBAB==0

* values

by countryyearB: egen totval_expBAA=sum(flowBAA)
by countryyearB: egen totval_expBAB=sum(flowBAB)

by countryyearB: egen totval_expBAA_cons=sum(flowBAA) if consistent_expBA==1
by countryyearB: egen totval_expBAA_cons2=max(totval_expBAA_cons)

by countryyearB: egen totval_expBAB_cons=sum(flowBAB) if consistent_expBA==1
by countryyearB: egen totval_expBAB_cons2=max(totval_expBAB_cons)

by countryyearB: egen totval_expBAA_consnopr=sum(flowBAA) if consistentnopr_expBA==1
by countryyearB: egen totval_expBAA_consnopr2=max(totval_expBAA_consnopr)

by countryyearB: egen totval_expBAB_consnopr=sum(flowBAB) if consistentnopr_expBA==1
by countryyearB: egen totval_expBAB_consnopr2=max(totval_expBAB_consnopr)

*
label variable totval_expBAA_consnopr2 "Monadic aggregate mirror exports, partners"
label variable totval_expBAB_consnopr2 "Monadic aggregate mirror exports, home"


** Keep only if receiving country is US and sending country is Canada
keep if countryA_alpha2cc=="US" | countryA_alpha2cc=="CA"		
drop if countryA_alpha2cc=="CA" & countryB_alpha2cc!="US"

** Keep only needed variables
keep countryB_alpha2cc countryB_name year totval_expBAA_consnopr2 totval_expBAB_consnopr2

save "monadicexports.dta", replace

* MERGE IMPORTS & EXPORTS

use monadicimports, clear
rename countryA_alpha2cc country_code
rename countryA_name country_name

gen countryyear = country_code + "_" + string(year)
save, replace


* 
use monadicexports, clear
rename countryB_alpha2cc country_code
rename countryB_name country_name
gen countryyear = country_code + "_" + string(year)

save, replace
*
use monadicimports, clear

merge 1:1 countryyear using monadicexports
drop if _merge!=3
drop _merge

save monadictrade, replace

** Keep only needed variables
keep country_code country_name year totval_impBAA_consnopr2 totval_impBAB_consnopr2 totval_expBAA_consnopr totval_expBAB_consnopr2

** Label variables
label variable country_code "Alpha-2 code"
label variable country_name "Country name"
label variable year "Year"

** Rename variables
rename totval_impBAA_consnopr2 mirrorimports_home
rename totval_impBAB_consnopr2 mirrorimports_partners
rename totval_expBAA_consnopr2 mirrorexports_partners
rename totval_expBAB_consnopr2 mirrorexports_home

** generate total trade
generate mirrortrade_home=mirrorimports_home+mirrorexports_home
generate mirrortrade_partners=mirrorimports_partners+mirrorexports_partners

label variable mirrortrade_home "Monadic aggregate mirror trade, home"
label variable mirrortrade_partners "Monadic aggregate mirror trade, partners"

* generate monadic ABBA terms

* imports
generate importABBA_relimp=((abs(mirrorimports_home-mirrorimports_partners))/((mirrorimports_home+mirrorimports_partners)/2))
label variable importABBA_relimp "imports ABBA relative to total value of imports (average of home and partner estimates)"

* exports
generate exportABBA_relexp=((abs(mirrorexports_home-mirrorexports_partners))/((mirrorexports_home+mirrorexports_partners)/2))
label variable exportABBA_relexp "exports ABBA relative to total value of exports (average of home and partner estimates)"

* total trade
generate tradeABBA_reltrade=((abs(mirrortrade_home-mirrortrade_partners))/((mirrortrade_home+mirrortrade_partners)/2))
label variable tradeABBA_reltrade "total trade ABBA relative to total value of trade (average of home and partner estimates)"


** drop if country-year row has no trade values
drop if missing(mirrorimports_home) & missing(mirrorimports_partners) & missing(mirrorexports_partners) & missing(mirrorexports_home) & missing(mirrortrade_home) & missing(mirrortrade_partners) &  missing(importABBA_relimp) &  missing(exportABBA_relexp) & missing(tradeABBA_reltrade)


save mirrortrade_monadic, replace
export delimited mirrortrade_monadic, replace

******
* END
******